package com.sdry.product.prehangingline.controller;
import	java.util.HashSet;
import	java.io.IOException;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdry.basic.entity.Material;
import com.sdry.basic.entity.ProductionStation;
import com.sdry.basic.entity.Pylons;
import com.sdry.basic.mapper.PylonsMapper;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.common.BaseController;
import com.sdry.common.ResultAppInfo;
import com.sdry.exception.BizException;
import com.sdry.product.prehangingline.service.PreHangingLineService;
import com.sdry.product.prehangingline.service.PreOneLineAppService;
import com.sdry.product.prod.entity.LacquerInto;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.mapper.LacquerIntoMapper;
import com.sdry.product.prod.service.ProductHandleOutService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description app端预挂线
 * @Author chenBin
 * @Date 2020-04-22 14:55
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "preOneLineApp")
public class PreOneLineAppController extends BaseController {

    /**挂架服务**/
    @Resource
    private PylonsMapper pylonsDao;

    @Resource
    private LacquerIntoMapper lacquerIntoDao;

    @Resource
    private PreHangingLineService preHangingLineService;

    @Resource
    private PreOneLineAppService preOneLineAppService;

    @Resource
    private ProductHandleOutService productHandleOutService;


    /**
     * 预挂线工位绑定,提交
     * 1.工位
     * 2.挂架
     * 3.物料
     * [{"psId": "12","psnumber":"89-21","pyId":"20","list": [{"maId": "9","quantity": "200","maSprayingRequirements":"银粉","remark04":"FJ"}]}]
     * {
     * "list": [
     * {
     * "quantity": "298",
     * "pyId": "0",
     * "oldPsnumber": "2-1",
     * "psId": "12",
     * "maSprayingRequirements": "黄色",
     * "maId": "9",
     * "remark04": "FJ"
     * },
     * {
     * "quantity": "300",
     * "pyId": "0",
     * "oldPsnumber": "2-1",
     * "psId": "12",
     * "maSprayingRequirements": "炉石",
     * "maId": "12",
     * "remark04": "QCL"
     * }
     * ],
     * "psnumber": "1585642171299"
     * }
     */
    @RequestMapping(value = "/api/addPreOneLine")
    public ResultAppInfo addPreOneLine(String json, String userId) {
        logger.info("预挂线工位绑定,提交","json = {} "+json);
        try {
            preOneLineAppService.addPreOneLine(json,userId);
            return getResultAppInfo("1",null,"");
        } catch (BizException e) {
            e.printStackTrace();
            return getResultAppInfo("0",null, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return getResultAppInfo("0",null, BaseAPIMessageTypeEnum.A001.getName());
        }
    }

    /**
     * 查询挂架
     * [
     *     {
     *         "createDate": 1583832611233,
     *         "delFlag": "0 ",
     *         "pyId": 2,
     *         "pyNumber": "111111",
     *         "pyName": "22222"
     *     }
     * ]
     */
    @RequestMapping(value = "/api/queryPylonsList")
    public List<Pylons> queryPylonsList() {
        logger.info("查询挂架 -- /api/queryPylonsList");
        QueryWrapper<Pylons> queryWrapper = new  QueryWrapper();
        queryWrapper.eq("del_flag",0L);
        return pylonsDao.selectList(queryWrapper);
    }

    /**
     * 查询工位信息
     * [
     *     {
     *         "page": 0,
     *         "pageSize": 10,
     *          "total": 0,
     *         "start": 0,
     *         "psid": 20,           工位ID
     *         "psnumber": "89-21"   工位编号
     *     }
     * ]
     */
    @RequestMapping(value = "/api/queryWorkstation")
    public List<ProductionStation> queryWorkstation(String type) {
        logger.info("预挂线 -- 查询工位信息--/api/queryWorkstation");
        List<ProductionStation> productionStationList = new ArrayList<> ();
        ProductionStation production = new ProductionStation();
        production.setPsid(20L);
        production.setPsnumber("89-21");
        productionStationList.add(production);
        return productionStationList;
    }

    /**
     * 查询物料，来自分拣区或者前处理区
     *
     */
    @RequestMapping(value = "/api/queryRegionMateriel")
    public ResultAppInfo queryRegionMateriel(String regionType,String permissionType) {
        logger.info("预挂线或者喷涂 - 查询物料，来自分拣区或者前处理区 ");
        List<ResultAppList<Material>> resultAppListList = new ArrayList<>();
        //前处理区
        if ("QCL".equals(regionType)) {
            //前处理区物料查询去往预挂线查询
            resultAppListList = productHandleOutService.queryRegionPretreatmentMateriel(permissionType);
        }else if ("FJ".equals(regionType)){
            //分拣区物料查询去往预挂线查询
            resultAppListList = preOneLineAppService.queryRegionPreHangingLineMateriel(permissionType);
        }else if ("WZZ".equals(regionType)){
            resultAppListList = preOneLineAppService.queryTransitreaMateriel();
        }
        return getResultAppInfo("1",resultAppListList,"");
    }

    /**
     * 紧急或者一般
     */
    @RequestMapping(value = "/api/queryAttribute")
    public ResultAppInfo queryAttribute() {
        logger.info("紧急或者一般,/api/queryAttribute");
        List<String> list = new ArrayList<String> ();
        list.add("紧急");
        list.add("正常");
        return getResultAppInfo("1",list,"");
    }

    /**
     * 用于预挂线交换工位查询物料
     */
    @RequestMapping(value = "/api/queryPreOneLineMateriel")
    public ResultAppInfo queryPreOneLineMateriel(String psnumber) {
        logger.info("用于预挂线交换工位查询物料-/api/queryPreOneLineMateriel");
        try {
            List<ResultAppList<Material>> resultAppListList = preOneLineAppService.queryPreOneLineMateriel(psnumber);
            return getResultAppInfo("1",resultAppListList,"");
        } catch (Exception e) {
            e.printStackTrace();
            return getResultAppInfo("0",null,BaseAPIMessageTypeEnum.A001.getName());
        }
    }
}
